/**
 * Copyright 2011 Honeywell Federal Manufacturing & Technologies, LLC. This material was produced under U.S. Government
 * contract DE-ACO4-01AL66850, Honeywell Federal Manufacturing & Technologies, LLC, (FM&T) with the U. S. Department of
 * Energy for the operation of the Kansas City Plant . The U.S. Government has rights to use, reproduce, and distribute
 * this software.  NEITHER THE GOVERNMENT NOR Honeywell Federal Manufacturing & Technologies, LLC, MAKES ANY WARRANTY,
 * EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE.  If software is modified to produce
 * derivative works, such modified software should be clearly marked, so as not to confuse it with the version available
 * from FM&T.
 *
 * Additionally, this program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License Version 3 published by the Free Software Foundation. Accordingly, this program is distributed in
 * the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this library.
 * If not, see http://www.gnu.org/licenses/.
 */
package org.braintrain.tapestry.view.components;

import org.apache.tapestry.json.JSONLiteral;
import org.apache.tapestry.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * @author John Ericksen
 */
public class TinyMCEConfigurationFactory {

    private HashMap<String, String> configuration;

    public TinyMCEConfigurationFactory() {

        configuration = new HashMap<String, String>();

        //default configuration
        // General options
        addConfiguration("mode", "textareas");
        addConfiguration("theme", "advanced");
        addConfiguration("plugins", "imgmap, safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount");

        // Theme options
        addConfiguration("theme_advanced_buttons1", "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect");
        addConfiguration("theme_advanced_buttons2", "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image, imgmap,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor");
        addConfiguration("theme_advanced_buttons3", "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,ltr,rtl,|,fullscreen");
        addConfiguration("theme_advanced_toolbar_location", "top");
        addConfiguration("theme_advanced_toolbar_align", "left");
        addConfiguration("theme_advanced_path_location", "bottom");
        addConfiguration("content_css", "css/tinyMCE.css");
        addConfiguration("plugin_insertdate_dateFormat", "%Y-%m-%d");
        addConfiguration("plugin_insertdate_timeFormat", "%H:%M:%S");
        addConfiguration("extended_valid_elements", "map[id|name],area[shape|alt|coords|href|target|onmouseover|onmouseout],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style],img[usemap|class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name|obj|param|embed]");
        addConfiguration("template_external_list_url", "script/template_list.js");
        addConfiguration("theme_advanced_resize_horizontal", "true");
        addConfiguration("file_browser_callback", "braintrainFileBrowser");
        addConfiguration("theme_advanced_resizing", "true");
    }

    /**
     * Replaces or adds the given configuration key and value to the configuration map.
     *
     * @param key
     * @param value
     * @return this configuration factory for chaining.
     */
    public TinyMCEConfigurationFactory addConfiguration(String key, String value) {
        configuration.put(key, value);

        return this;
    }

    public String getConfigurationValue(String key) {
        return configuration.get(key);
    }

    public JSONObject buildConfiguration() {

        JSONObject jsonObject = new JSONObject();

        for(Map.Entry<String, String> entry : configuration.entrySet()){
            jsonObject.put(new JSONLiteral(entry.getKey()), entry.getValue());
        }

        return jsonObject;
    }
}
